package com.lagou;

import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;

import java.io.File;
import java.io.FileOutputStream;

/**
 * @author zy.du
 * @date 2020/11/11 1:37 PM
 */
public class FastdfsTest {
    public static void main(String[] args) {

        //testUpload();
        //testQuery();
        testDownload();
    }

    static void testUpload() {
        try {
            //加载配置文件
            ClientGlobal.initByProperties("fastdfs-client.properties");
            System.out.println("ClientGlobal.configInfo(): " + ClientGlobal.configInfo());
            //创建tracker客户端
            TrackerClient tc = new TrackerClient();
            //根据tracker客户端创建连接 获取到跟踪服务器对象
            TrackerServer ts = tc.getConnection();
            StorageServer ss = null;
            //定义storage客户端
            StorageClient1 client = new StorageClient1(ts, ss); //文件元信息
            NameValuePair[] list = new NameValuePair[1];
            list[0] = new NameValuePair("fileName", "JVM加载.png");
            // 上传，返回fileId
            String fileId = client.upload_file1("/Users/apple/Desktop/技术插图/JVM加载.png", "png", list);
            //group1/M00/00/00/wKg4zF-rAc-ARGr5AAv0Ob1oBA8133.png
            //http://192.168.56.204:8888/group1/M00/00/00/wKg4zF-qjnWADljzAAzo4Ris_P0357.jpg
            System.out.println(fileId);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    static public void testQuery() {
        try {
            //加载配置文件
            ClientGlobal.initByProperties("fastdfs-client.properties");
            // 创建tracker客户端
            TrackerClient tc = new TrackerClient();
            // 根据tracker客户端创建连接 获取到跟踪服务器对象
            TrackerServer ts = tc.getConnection();
            StorageServer ss = null;
            //定义storage客户端
            StorageClient1 client = new StorageClient1(ts, ss);
            // 查询文件信息
            FileInfo fileInfo = client.query_file_info1("group1/M00/00/00/wKg4zF-rAc-ARGr5AAv0Ob1oBA8133.png");
            System.out.println(fileInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    static void testDownload() {
        try {
            // 加载配置文件
            ClientGlobal.initByProperties("fastdfs-client.properties");
            // 创建tracker客户端
            TrackerClient tc = new TrackerClient();
            // 根据tracker客户端创建连接
            TrackerServer ts = tc.getConnection();
            StorageServer ss = null;
            // 定义storage客户端
            StorageClient1 client = new StorageClient1(ts, ss);
            // 下载
            byte[] bs = client.download_file1
                    ("group1/M00/00/00/wKg4zF-rAc-ARGr5AAv0Ob1oBA8133.png");
            FileOutputStream fos = new FileOutputStream(new File("test111.png"));
            fos.write(bs);
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
